#include "threadPool.h"
void * threadFunc(void * arg)
{
    printf("worker准备就绪\n");
    threadPool_t * pthreadPool=(threadPool_t *)arg;
    while(1)
    {
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->exitFlag==0&&pthreadPool->taskQueue.queueSize<=0)
        {
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);            
        }
        if(pthreadPool->exitFlag==1)
        {
            printf("i am worker becaust signal of  i goint to eixt\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            exit(0);
        }

        int netfd=pthreadPool->taskQueue.pFront->netfd;
        printf("i am worker i get netfd%d\n",netfd);
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        

        //在外面初始化用户当前工作目录
        stringStack_t stack;
        stringStackInit(&stack);
        int enmpty_num=0;
        while(1)
        {
            train_t train;
            memset(&train, 0, sizeof(train));
            char temp[1024] = {0};
            int ret = recv(netfd, &train, sizeof(train), 0);
            if(enmpty_num>10)break;
            if(ret <= 0)
            {
                enmpty_num++;
                printf("接受失败\n");
                continue;
            }
            printf("train.flag = %d, train.length = %d, train.data = %s\n", train.flag,train.length,train.data);
            char server_work_path[200]={0};
            strcat(server_work_path,ROOT_PATH);
            char client_work_path[100]={0};
            func_pwd(client_work_path,&stack);
            strcat(server_work_path,client_work_path);
            char true_file_path[300]={0};
            strcat(true_file_path,server_work_path);//gets puts mkdir rmdir remove 用到
            if(strlen(train.data)>0)strcat(true_file_path,"/");
            strcat(true_file_path,train.data);
            printf("true_file_path :%s\n",true_file_path);
            //cd
            if(train.flag ==1)
            {
                ssize_t ret =  func_cd(train.data,&stack);
                if(ret == 0) send(netfd, "0", 1,  MSG_NOSIGNAL);
                else send(netfd, "-1",2, MSG_NOSIGNAL);
            }
            //ls
            else if(train.flag == 2)
            {
                ssize_t ret = func_ls(temp, &stack);
                if(ret == 0) send(netfd, temp, strlen(temp), MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }
            //puts  客户端puts上传
            else if(train.flag == 3)
            {
                ssize_t ret = func_gets(netfd,true_file_path, &stack);
                if(ret == 0) send(netfd, "0", 1, MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }
            //gets  客户端gets下载
            else if(train.flag == 4)
            {
                printf("用户正在下载\n");
                ssize_t ret = func_puts(netfd,true_file_path,train.data, &stack);
                if(ret == 0) send(netfd, "0", 1, MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }
            //remove 删除服务器的文件
            else if(train.flag == 5)
            {
                ssize_t ret = func_remove(true_file_path, &stack);
                if(ret == 0) send(netfd, "0", 1, MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }
            //pwd
            else if(train.flag == 6)
            {
                ssize_t ret = func_pwd(temp, &stack);
                if(ret == 0)send(netfd, temp, strlen(temp), MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }
            //mkdir
            else if(train.flag == 7)
            {
                ssize_t ret = func_mkdir(true_file_path,&stack);
                if(ret == 0) send(netfd, "0", 1,  MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }
            //rmdir
            else if(train.flag == 8)
            {
                ssize_t ret =  func_rmdir(true_file_path,&stack);
                if(ret == 0) send(netfd, "0", 1, MSG_NOSIGNAL);
                else send(netfd, "-1", 2, MSG_NOSIGNAL);
            }  
        }//内层will
        close(netfd);
    }//外层while
        return NULL;
}
 //创建子线程
 int makeworker(threadPool_t * pthreadPool)
 {
     for(int i=0;i<pthreadPool->tidArr.workNum;i++)
     {
         pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
     }
     return 0;
 }
